package com.hckjsec.oa.controller;

import com.alibaba.fastjson.JSON;
import com.hckjsec.common.core.controller.BaseController;
import com.hckjsec.common.core.domain.AjaxResult;
import com.hckjsec.common.core.domain.entity.SysUser;
import com.hckjsec.common.general.MapLongNameView;
import com.hckjsec.common.utils.*;
import com.hckjsec.oa.domain.*;
import com.hckjsec.oa.domain.Home.OaHomeData;
import com.hckjsec.oa.domain.Home.ProgrammeHome;
import com.hckjsec.oa.domain.Home.ToBeReviewedView;
import com.hckjsec.oa.domain.OaView.AppRequestView;
import com.hckjsec.oa.flow.FlowPath;
import com.hckjsec.oa.service.*;
import com.hckjsec.system.domain.SysNotice;
import com.hckjsec.system.domain.SysPost;
import com.hckjsec.system.service.ISysNoticeService;
import com.hckjsec.system.service.ISysPostService;
import com.hckjsec.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@RestController
@RequestMapping("/oa/home")
public class OaHomeController extends BaseController {
    @Autowired
    private IOaProgrammeService oaProgrammeService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysNoticeService noticeService;
    @Autowired
    private ISysPostService postService;
    @Autowired
    private IOaApplyService oaApplyService;
    @Autowired
    private IOaAuditingService oaAuditingService;
    @Autowired
    private IOaLeaveService oaLeaveService;
    @Autowired
    private IOaMissionService oaMissionService;
    @Autowired
    private IOaExpenseService oaExpenseService;

    /**
     * OA生日,通知公告
     * @author cyt
     * @date 2021-06-23
     */
    @GetMapping("/getHomeBirth")
    public AjaxResult getHomeBirth() {
        OaHomeData oaHomeData = new OaHomeData();
        SysNotice notice = new SysNotice();
        notice.setStatus("0");
        List<SysNotice> noticeList = noticeService.selectNoticeList(notice);
        oaHomeData.setNoticeList(noticeList);
        List<SysUser> sysUsersList =  userService.initUserList();
        Map<Date,List<MapLongNameView>> proHt  = new HashMap<>();
        sysUsersList.forEach(kv->{
            Date date = kv.getBirthDate();
            if (StringUtils.isNotNull(date)){
                List<MapLongNameView> proList = new ArrayList<>();
                if (proHt.containsKey(date)){
                    proList = proHt.get(date);
                }
                MapLongNameView nameView = new MapLongNameView();
                nameView.setName(kv.getNickName());
                nameView.setValue(null);
                proList.add(nameView);
                proHt.put(date,proList);
            }
        });
        List<ProgrammeHome> birthdayList = new ArrayList<>();
        proHt.forEach((k,v)->{
            ProgrammeHome programmeHome = new ProgrammeHome();
            programmeHome.setDate(k);
            programmeHome.setUserList(v);
            birthdayList.add(programmeHome);
        });
        oaHomeData.setBirthdayList(birthdayList);
        return AjaxResult.success(oaHomeData);
    }

    /**
     * 日程
     * @author cyt
     * @date 2021-06-23
     */
    @PostMapping("/getHomeProgramme")
    public AjaxResult getHomeProgramme(@RequestBody AppRequestView appRequestView) {
        String date = StringUtils.isHcNull(appRequestView.getDate(),"");
        if("".equals(date)){
            date = DateUtils.parseDateToStr(DateUtils.YYYY_MM_DD,DateUtils.getNowDate());//当前时间
        }
        OaProgramme oaProgramme = new OaProgramme();
        oaProgramme.setUserId(SecurityUtils.getUserId());
        oaProgramme.setBeginTime(date);
        oaProgramme.setEndTime(date);
        List<OaProgramme> list = oaProgrammeService.selectOaProgrammeList(oaProgramme);
        return AjaxResult.success(list);
    }


    /**
     * 待审核列表
     * @author cyt
     * @date 2021-06-23
     */
    @GetMapping("/tobeReviewedList")
    public AjaxResult tobeReviewedList() {
        List<ToBeReviewedView> list = new ArrayList<>();
        List<String> codeList = new ArrayList<>();
        Long userId = SecurityUtils.getUserId();
        List<Integer> postList = postService.selectPostListByUserId(userId);
        List<SysPost> postAllList = postService.selectPostList(null);
        postAllList.forEach(kv -> {
            if (postList.contains(kv.getPostId().intValue())) {
                codeList.add(kv.getPostCode());
            }
        });
       //加班管理
        OaApply  oldOaApply = new OaApply();
        oldOaApply.setState("0");
        List<OaApply> oaApplyList = oaApplyService.selectOaApplyList(oldOaApply);
        oaApplyList.forEach(kv->{
            OaApply oaApply = oaApplyService.selectOaApplyById(kv.getId());
            OaAuditing oaAuditing = new OaAuditing();
            oaAuditing.setType("overtime");
            oaAuditing.setOutsideId(oaApply.getId());
            List<OaAuditing> auditingList = oaAuditingService.selectOaAuditingList(oaAuditing);
            boolean isAuditor = false;
            if (!StringUtils.isEmpty(auditingList)) {
                isAuditor = FlowPath.isAuditing(codeList, "overtime", auditingList.get(auditingList.size() - 1).getSchedule());
            } else {
                isAuditor = FlowPath.isAuditing(codeList, "overtime", "");
            }
            if (isAuditor){
                list.add(new ToBeReviewedView("workovertime","加班申请",oaApply.getId(),oaApply.getUserName(),oaApply.getCreateTime()));
            }
        });
        //请假管理
        OaLeave oldOaLeave = new OaLeave();
        oldOaLeave.setState("0");
        List<OaLeave> oaLeaveList = oaLeaveService.selectOaLeaveList(oldOaLeave);
        oaLeaveList.forEach(kv->{
            OaLeave oaLeave = oaLeaveService.selectOaLeaveById(kv.getId());
            OaAuditing oaAuditing = new OaAuditing();
            oaAuditing.setType("leave");
            oaAuditing.setOutsideId(oaLeave.getId());
            List<OaAuditing> auditingList = oaAuditingService.selectOaAuditingList(oaAuditing);
            boolean isAuditor = false;
            if (!StringUtils.isEmpty(auditingList)) {
                isAuditor = FlowPath.isAuditing(codeList, "leave", auditingList.get(auditingList.size() - 1).getSchedule());
            } else {
                isAuditor = FlowPath.isAuditing(codeList, "leave", "");
            }
            if (isAuditor){
                list.add(new ToBeReviewedView("leave","请假申请",oaLeave.getId(),oaLeave.getUserName(),oaLeave.getCreateTime()));
            }
        });
        //出差
        OaMission oldOaMission = new OaMission();
        oldOaMission.setState("0");
        List<OaMission> oaMissionList = oaMissionService.selectOaMissionList(oldOaMission);
        oaMissionList.forEach(kv->{
            OaMission oaMission = oaMissionService.selectOaMissionById(kv.getId());
            OaAuditing oaAuditing = new OaAuditing();
            oaAuditing.setType("mission");
            oaAuditing.setOutsideId(oaMission.getId());
            List<OaAuditing> auditingList = oaAuditingService.selectOaAuditingList(oaAuditing);
            boolean isAuditor = false;
            if (!StringUtils.isEmpty(auditingList)) {
                isAuditor = FlowPath.isAuditing(codeList, "mission", auditingList.get(auditingList.size() - 1).getSchedule());
            } else {
                isAuditor = FlowPath.isAuditing(codeList, "mission", "");
            }
            if (isAuditor){
                list.add(new ToBeReviewedView("trip","出差申请",oaMission.getId(),oaMission.getUserName(),oaMission.getCreateTime()));
            }
        });
        //报销
        OaExpense oldOaExpense = new OaExpense();
        oldOaExpense.setState("0");
        List<OaExpense> oaExpenseList = oaExpenseService.selectOaExpenseList(oldOaExpense);
        oaExpenseList.forEach(kv->{
            OaExpense oaExpense = oaExpenseService.selectOaExpenseById(kv.getId());
            OaAuditing oaAuditing = new OaAuditing();
            oaAuditing.setType("expense");
            oaAuditing.setOutsideId(oaExpense.getId());
            List<OaAuditing> auditingList = oaAuditingService.selectOaAuditingList(oaAuditing);
            boolean isAuditor = false;
            if (!StringUtils.isEmpty(auditingList)) {
                isAuditor = FlowPath.isAuditing(codeList, "expense", auditingList.get(auditingList.size() - 1).getSchedule());
            } else {
                isAuditor = FlowPath.isAuditing(codeList, "expense", "");
            }
            if (isAuditor){
                list.add(new ToBeReviewedView("expense","报销申请",oaExpense.getId(),oaExpense.getUserName(),oaExpense.getCreateTime()));
            }
        });
        return AjaxResult.success(list);
    }
}
